import { Component, EventEmitter, Injector, Output, OnInit, ViewChild, Input } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { ActivatedRoute } from '@angular/router';
import { AppComponentBase } from '@shared/common/app-component-base';
import { TaskServiceProxy, WorkflowServiceProxy, OutsourcingWorkFlowServiceProxy, UserServiceProxy, ProcessingDeliveryDto, ProcessingDeliveryViewDto , ProcessingOutPutDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { FileDownloadService} from '@shared/utils/file-download.service';
import * as _ from 'lodash';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';

@Component({
  selector: 'ProcessingDeliveryDetail',
  templateUrl: './ProcessingDeliveryDetail.component.html',
  styleUrls: ['./ProcessingDeliveryDetail.component.css']
})
export class ProcessingDeliveryDetailComponent  extends AppComponentBase implements OnInit {
  @ViewChild('createOrEditModal') modal: ModalDirective;
  @ViewChild('paginator') paginator: Paginator;
  @ViewChild('taskdataTable') taskdataTable: Table;
  @ViewChild('dataTable') dataTable: Table;

  @Input() workflowid: any;
  @Output() out = new EventEmitter();

  active = false;
  saving = false;
  _processingDeliveryDto: ProcessingDeliveryDto = new ProcessingDeliveryDto();
  _processingDeliveryViewDto: ProcessingDeliveryViewDto[];
  detailsprimengTableHelper: PrimengTableHelper;
  ngOnInit(): void {

    console.log(this.workflowid);
    this.initForm();
  }
  constructor(injector: Injector,
    private route: ActivatedRoute,
    private _OutsourcingWorkFlowServiceProxy: OutsourcingWorkFlowServiceProxy,
    private _taskServiceProxy: TaskServiceProxy,
    private _fileDownloadService: FileDownloadService,
    private _userServiceProxy: UserServiceProxy,
    private _workflowService: WorkflowServiceProxy) {
    super(injector);
    this.detailsprimengTableHelper = new PrimengTableHelper();

  }

  initForm(): void {
    this._OutsourcingWorkFlowServiceProxy.getProcessingDeliveryByWorkFlowId(this.workflowid).subscribe(dto => {
      this._processingDeliveryDto = dto.processingDeliveryDto;
      this._processingDeliveryViewDto = dto.processingDeliveryViewDtos;
      console.log(dto);
      this.primengTableHelper.totalRecordsCount = this._processingDeliveryViewDto.length;
      this.primengTableHelper.records = this._processingDeliveryViewDto;
      this.primengTableHelper.hideLoadingIndicator();

      _.forEach(this._processingDeliveryViewDto, viewItem => {
        console.log(viewItem.taskForModelId);
        this._taskServiceProxy.getTaskForEdit(viewItem.taskForModelId)
          .subscribe(userResult => {
            console.log(userResult);
            viewItem.taskCode = userResult.taskCode;
            viewItem.taskName = userResult.drawingName;
            viewItem.picNo = userResult.drawingCode;
          });

        this.active = true;
      });
    });
  }

  exportToexcel(): void {
    this._OutsourcingWorkFlowServiceProxy.processingDeliveryExicForExcel(this.workflowid).subscribe(dto => {
        this._fileDownloadService.downloadTempFile(dto);
    });
  }
}
